# -*- coding: utf-8 -*-


from wolfox.source.fxj import DayExtractor,XExtractor,ReportExtractor
from wolfox.source.transfer import createFilter
import wolfox.source.reader as reader


def get_day_info(filename,extractor,begin=0,end=99999999,stocks=None):
    ''' 获得指定文件在指定时间段内的记录数'''
    ri = reader.Reader(extractor,createFilter(begin,end,stocks))
    quotes = ri.read(filename)
    if(not quotes):    print >> output,'在%d - %d 之间没有指定的数据' %(begin,end)
    quotes.sort()
    stock_records = {}
    dispatch = lambda item: stock_records.setdefault(item.tstock,[]).append(item)
    map(dispatch,quotes)    #temp的每个key都对应为排过序列表
    from_date,to_date,nums = quotes[0].tdate,quotes[-1].tdate,len(quotes)
    return (from_date,to_date,nums),stock_records

if __name__ == '__main__':
    import optparse
    import cStringIO

    parser = optparse.OptionParser()
    parser.add_option('--source','-s',help="需要显示信息的源记录文件")
    options,arguments = parser.parse_args()

    if options.source:
        output = cStringIO.StringIO()
        sum_info,stock_records = get_day_info(options.source,DayExtractor())
        rlist = []
        for key,records in stock_records.items():
            rlist.append((key,records[0].tdate,records[-1].tdate,len(records)))
            #print >> output, '%s 共有从%s - %s 的 %d 条记录' % (key,records[0].tdate,records[-1].tdate,len(records))
            rlist.sort(key=lambda x:x[0])

        for item in rlist:  #输出到output
            print >> output,item
        print output.getvalue(),sum_info
    else:
        print u'请输入目标文件名'
